package com.springboot.template.thread.synchronizedDemo;


class Seat1 implements Runnable {
    private int position = 100;

    @Override
    public void run() {
        while (true){
            int position1 = getPosition();
            if (position1 <=0) {
                break;
            }
        }
    }

    //默认同步监视器：this
    private synchronized int getPosition(){
        if(position > 0){
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            System.out.println("从"+Thread.currentThread().getName() + "得到座位，座位号为：" + position);
            --position;
        }
        return position;
    }
}

public class TestRunnable {
    public static void main(String[] args) {
        Seat1 seat = new Seat1();
        Thread thread = new Thread(seat);
        Thread thread1 = new Thread(seat);
        Thread thread2 = new Thread(seat);

        thread.setName("渠道1");
        thread1.setName("渠道2");
        thread2.setName("渠道3");

        thread.start();
        thread1.start();
        thread2.start();
    }
}
